package com.gt.notepad;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainBrowser extends ListActivity {
	private List<String> items = null;
	final  String DEF_FOLDER="/sdcard";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFiles(new File(DEF_FOLDER).listFiles());
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	 int selectedRow = (int)id;
    	    if(selectedRow == 0){
    	    	//if selected ../
    	        getFiles(new File("/").listFiles());
    	    }else{
    	        File file = new File(items.get(selectedRow));
    	        if(file.isDirectory()){
    	            getFiles(file.listFiles());
    	        }else{
    	             Toast.makeText(this, file.getName(), 1000);
    	        }
    	    }
    }
    private void getFiles(File[] files){
        items = new ArrayList<String>();
        items.add("../(To ROOT)");
        for(File file : files){
            items.add(file.getName());
        }
        setListAdapter(new ArrayAdapter<String>(this,R.layout.list_layout, items));
    }
}